{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "pd.set_option('display.max_rows', 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../raw_data/triples.csv'\n",
    "df = pd.read_csv(path, header=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>tail</th>\n",
       "      <th>relation</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>关羽</td>\n",
       "      <td>刘备</td>\n",
       "      <td>younger_sworn_brother</td>\n",
       "      <td>义弟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张飞</td>\n",
       "      <td>刘备</td>\n",
       "      <td>younger_sworn_brother</td>\n",
       "      <td>义弟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>关羽</td>\n",
       "      <td>张飞</td>\n",
       "      <td>elder_sworn_brother</td>\n",
       "      <td>义兄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>张苞</td>\n",
       "      <td>张飞</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>关兴</td>\n",
       "      <td>关羽</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>关平</td>\n",
       "      <td>张苞</td>\n",
       "      <td>sworn</td>\n",
       "      <td>结拜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>关平</td>\n",
       "      <td>关羽</td>\n",
       "      <td>adopted_son</td>\n",
       "      <td>义子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>卢植</td>\n",
       "      <td>刘备</td>\n",
       "      <td>master</td>\n",
       "      <td>师傅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>公孙瓒</td>\n",
       "      <td>刘备</td>\n",
       "      <td>friend</td>\n",
       "      <td>朋友</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>甘氏</td>\n",
       "      <td>刘备</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>刘禅</td>\n",
       "      <td>甘氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>刘禅</td>\n",
       "      <td>son_in_law</td>\n",
       "      <td>女婿</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>姜维</td>\n",
       "      <td>master</td>\n",
       "      <td>师傅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>刘备</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>lord</td>\n",
       "      <td>主公</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马师</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马懿</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>司马炎</td>\n",
       "      <td>司马昭</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>曹仁</td>\n",
       "      <td>曹操</td>\n",
       "      <td>same_clan</td>\n",
       "      <td>同族</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>曹纯</td>\n",
       "      <td>曹仁</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>曹操</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>刘氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>超昂</td>\n",
       "      <td>刘氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>卞氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>曹丕</td>\n",
       "      <td>卞氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>曹植</td>\n",
       "      <td>曹丕</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>马谡</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>colleague</td>\n",
       "      <td>同事</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150</th>\n",
       "      <td>马谡</td>\n",
       "      <td>刘备</td>\n",
       "      <td>minister</td>\n",
       "      <td>臣</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>孙坚</td>\n",
       "      <td>孙权</td>\n",
       "      <td>father</td>\n",
       "      <td>父亲</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152</th>\n",
       "      <td>吴国太</td>\n",
       "      <td>孙权</td>\n",
       "      <td>mother</td>\n",
       "      <td>母亲</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>153 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    head tail               relation label\n",
       "0     关羽   刘备  younger_sworn_brother    义弟\n",
       "1     张飞   刘备  younger_sworn_brother    义弟\n",
       "2     关羽   张飞    elder_sworn_brother    义兄\n",
       "3     张苞   张飞                    son    儿子\n",
       "4     关兴   关羽                    son    儿子\n",
       "5     关平   张苞                  sworn    结拜\n",
       "6     关平   关羽            adopted_son    义子\n",
       "7     卢植   刘备                 master    师傅\n",
       "8    公孙瓒   刘备                 friend    朋友\n",
       "9     甘氏   刘备                   wife     妻\n",
       "10    刘禅   甘氏                    son    儿子\n",
       "11   诸葛瞻   刘禅             son_in_law    女婿\n",
       "12   诸葛瞻  诸葛亮                    son    儿子\n",
       "13   诸葛亮   姜维                 master    师傅\n",
       "14    刘备  诸葛亮                   lord    主公\n",
       "..   ...  ...                    ...   ...\n",
       "138  司马昭  司马师        younger_brother    弟弟\n",
       "139  司马昭  司马懿                    son    儿子\n",
       "140  司马炎  司马昭                    son    儿子\n",
       "141   曹仁   曹操              same_clan    同族\n",
       "142   曹纯   曹仁        younger_brother    弟弟\n",
       "143   曹昂   曹操                    son    儿子\n",
       "144   刘氏   曹操                   wife    妻子\n",
       "145   超昂   刘氏                    son    儿子\n",
       "146   卞氏   曹操                   wife    妻子\n",
       "147   曹丕   卞氏                    son    儿子\n",
       "148   曹植   曹丕        younger_brother    弟弟\n",
       "149   马谡  诸葛亮              colleague    同事\n",
       "150   马谡   刘备               minister     臣\n",
       "151   孙坚   孙权                 father    父亲\n",
       "152  吴国太   孙权                 mother    母亲\n",
       "\n",
       "[153 rows x 4 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "超昂 应该是曹昂，之后把所有内容都更改一下\n",
    "\n",
    "145   超昂   刘氏      son    儿子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'超昂'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.at[145, 'head']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'曹昂'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.at[145, 'head'] = '曹昂'\n",
    "df.at[145, 'head']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "head_identicle_people = set(df['head'].tolist())\n",
    "tail_identicle_people = set(df['tail'].tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_identicle_people = set.union(head_identicle_people, tail_identicle_people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'刘禅', '马谡', '貂蝉', '凌统', '公孙越', '蔡瑁', '文聘', '太史慈', '诸葛瞻', '吴国太', '丁原', '大乔', '蒋琬', '张宝', '鲁肃', '王允', '丁奉', '徐庶', '卞氏', '刘表', '夏侯楙', '曹纯', '袁尚', '司马炎', '乐进', '蒋钦', '曹爽', '孟获', '黄盖', '周泰', '马腾', '刘辩', '孙尚香', '黄承彦', '糜氏', '甘氏', '卢植', '郭嘉', '吕布', '黄祖', '刘氏', '袁术', '司马师', '庞统', '曹昂', '法正', '徐晃', '钟会', '曹植', '庞德公', '徐盛', '马良', '张绣', '邓艾', '清河公主', '姜维', '董卓', '曹真', '孙氏', '刘备', '公孙瓒', '诸葛亮', '孙策', '韩当', '曹操', '曹丕', '韩遂', '高顺', '孙韶', '张昭', '黄月英', '曹嵩', '刘胜', '魏延', '张苞', '祝融', '张郃', '马超', '蔡氏', '张辽', '于禁', '诸葛瑾', '周瑜', '关平', '荀彧', '典韦', '袁熙', '关兴', '刘协', '蒯越', '曹仁', '程普', '孙权', '许褚', '夏侯淳', '袁谭', '荀攸', '司马昭', '程昱', '刘启', '庞德', '孙坚', '陈宫', '陆逊', '贾诩', '张飞', '吕蒙', '糜芳', '吕伯奢', '关羽', '糜竺', '张梁', '邹氏', '张角', '甘宁', '黄忠', '袁绍', '沙摩柯', '小乔', '司马懿', '夏侯渊', '赵云'}\n",
      "122\n"
     ]
    }
   ],
   "source": [
    "print(all_identicle_people)\n",
    "print(len(all_identicle_people))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_relation(name):\n",
    "    p1 = df.loc[df['head'] == name]\n",
    "    p2 = df.loc[df['tail'] == name]\n",
    "    print(p1)\n",
    "    print(p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    head tail relation label\n",
      "143   曹昂   曹操      son    儿子\n",
      "145   曹昂   刘氏      son    儿子\n",
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "find_relation( '曹昂' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n",
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "find_relation( '超昂' )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "夏侯惇被错写为了夏侯淳，更改所有的位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n",
      "    head tail      relation label\n",
      "114  夏侯渊  夏侯淳  son_of_uncle    堂弟\n",
      "115   曹操  夏侯淳     same_clan    同族\n",
      "116   曹操  夏侯淳          lord    主公\n"
     ]
    }
   ],
   "source": [
    "find_relation('夏侯淳')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.at[114, 'tail'] = '夏侯惇'\n",
    "df.at[115, 'tail'] = '夏侯惇'\n",
    "df.at[116, 'tail'] = '夏侯惇'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n",
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "find_relation('夏侯淳')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [head, tail, relation, label]\n",
      "Index: []\n",
      "    head tail      relation label\n",
      "114  夏侯渊  夏侯惇  son_of_uncle    堂弟\n",
      "115   曹操  夏侯惇     same_clan    同族\n",
      "116   曹操  夏侯惇          lord    主公\n"
     ]
    }
   ],
   "source": [
    "find_relation('夏侯惇')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一共有122个不同的武将。这里我们将这些武将分为4大势力，蜀国，魏国，吴国，群雄"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "魏国： '荀攸' '徐晃' '庞德'  '曹操'  '张郃' '荀彧' '司马昭' '曹仁' '曹植' '典韦' '郭嘉' '乐进' '张辽' '夏侯淳' '曹真' '曹爽' '曹纯' '司马炎' '司马懿' '邓艾' '夏侯渊' '贾诩' '许褚' '司马师' '夏侯楙' '蔡瑁' '曹嵩' '邹氏' '程昱' '刘氏' '清河公主' '于禁' '蒯越' '钟会' '文聘' '曹昂'\n",
    " \n",
    "蜀国：'关兴' '魏延' '诸葛亮' '诸葛瞻' '法正' '黄忠' '关羽' '赵云' '姜维' '张飞' '徐庶' '马良' '诸葛瑾'  '糜芳'  '刘禅' '黄月英' '庞统' '马超' '祝融'  '黄承彦' '沙摩柯' '甘氏' '糜竺' '刘备' '关平' '张苞' '糜氏' '蒋琬' '马谡' \n",
    " \n",
    "吴国：'孙坚' '太史慈' '孙策' '大乔' '周泰' '孙权' '黄盖' '周瑜' '甘宁' '吴国太'  '吕蒙' '曹丕' '曹昂' '鲁肃'  '孙尚香' '陆逊' '程普' '徐盛' '孙氏'  '韩当' '张昭' '孙韶' '蒋钦' '凌统' '丁奉' '小乔'\n",
    " \n",
    " \n",
    "群雄：'袁术' '马腾' '陈宫' '公孙瓒'  '吕布'  '袁绍' '董卓' '孟获' '貂蝉' '吕伯奢' '刘辩' '韩遂' '丁原' '张角' '袁谭' '刘胜' '公孙越' '张梁' '张宝'  '庞德公' '袁熙'  '刘表'  '卢植' '刘协' '王允' '袁尚' '高顺' '张绣' '刘启' '卞氏' '黄祖' '蔡氏'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "def string2list(country):\n",
    "    \"\"\"convert name string to name list\"\"\"\n",
    "    country = country.replace(\"'\", '')\n",
    "    return country.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "wei = \"'荀攸' '徐晃' '庞德'  '曹操'  '张郃' '荀彧' '司马昭' '曹仁' '曹植' '典韦' '郭嘉' '乐进' '张辽' '夏侯淳' '曹真' '曹爽' '曹纯' '司马炎' '司马懿' '邓艾' '夏侯渊' '贾诩' '许褚' '司马师' '夏侯楙' '蔡瑁' '曹嵩' '邹氏' '程昱' '刘氏' '清河公主' '于禁' '蒯越' '钟会' '文聘' '曹昂'\"\n",
    "shu = \"'关兴' '魏延' '诸葛亮' '诸葛瞻' '法正' '黄忠' '关羽' '赵云' '姜维' '张飞' '徐庶' '马良' '诸葛瑾'  '糜芳'  '刘禅' '黄月英' '庞统' '马超' '祝融'  '黄承彦' '沙摩柯' '甘氏' '糜竺' '刘备' '关平' '张苞' '糜氏' '蒋琬' '马谡'\"\n",
    "wu = \"'孙坚' '太史慈' '孙策' '大乔' '周泰' '孙权' '黄盖' '周瑜' '甘宁' '吴国太'  '吕蒙' '曹丕' '曹昂' '鲁肃'  '孙尚香' '陆逊' '程普' '徐盛' '孙氏'  '韩当' '张昭' '孙韶' '蒋钦' '凌统' '丁奉' '小乔'\"\n",
    "others = \"'袁术' '马腾' '陈宫' '公孙瓒'  '吕布'  '袁绍' '董卓' '孟获' '貂蝉' '吕伯奢' '刘辩' '韩遂' '丁原' '张角' '袁谭' '刘胜' '公孙越' '张梁' '张宝'  '庞德公' '袁熙'  '刘表'  '卢植' '刘协' '王允' '袁尚' '高顺' '张绣' '刘启' '卞氏' '黄祖' '蔡氏'\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "wei = string2list(wei)\n",
    "shu = string2list(shu)\n",
    "wu = string2list(wu)\n",
    "others = string2list(others)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(wei) + len(shu) + len(wu) + len(others)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "到此为止得到了保存有三个势力的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four_camp = wei + shu + wu + others \n",
    "len(four_camp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "four_camp的总人数是123人，说明有重复的一人。下面写个函数，来找到是哪个人加入了两个势力。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "曹昂\n",
      "曹昂\n"
     ]
    }
   ],
   "source": [
    "for people in four_camp:\n",
    "    count = four_camp.count(people)\n",
    "    if count == 2:\n",
    "        print(people)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "好的，发现是曹昂加入了吴国阵营。。下面重新设定一下人物列表。为了方便给之后的pandas添加新的一列，这里用dict来保存所有人的配属："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "wei = \"'荀攸' '徐晃' '庞德'  '曹操'  '张郃' '荀彧' '司马昭' '曹仁' '曹植' '典韦' '郭嘉' '乐进' '张辽' '夏侯惇' '曹真' '曹爽' '曹纯' '司马炎' '司马懿' '邓艾' '夏侯渊' '贾诩' '许褚' '司马师' '夏侯楙' '蔡瑁' '曹嵩' '邹氏' '程昱' '刘氏' '清河公主' '于禁' '蒯越' '钟会' '文聘' '曹昂'\"\n",
    "shu = \"'关兴' '魏延' '诸葛亮' '诸葛瞻' '法正' '黄忠' '关羽' '赵云' '姜维' '张飞' '徐庶' '马良' '诸葛瑾'  '糜芳'  '刘禅' '黄月英' '庞统' '马超' '祝融'  '黄承彦' '沙摩柯' '甘氏' '糜竺' '刘备' '关平' '张苞' '糜氏' '蒋琬' '马谡'\"\n",
    "wu = \"'孙坚' '太史慈' '孙策' '大乔' '周泰' '孙权' '黄盖' '周瑜' '甘宁' '吴国太'  '吕蒙' '曹丕' '鲁肃'  '孙尚香' '陆逊' '程普' '徐盛' '孙氏'  '韩当' '张昭' '孙韶' '蒋钦' '凌统' '丁奉' '小乔'\"\n",
    "others = \"'袁术' '马腾' '陈宫' '公孙瓒'  '吕布'  '袁绍' '董卓' '孟获' '貂蝉' '吕伯奢' '刘辩' '韩遂' '丁原' '张角' '袁谭' '刘胜' '公孙越' '张梁' '张宝'  '庞德公' '袁熙'  '刘表'  '卢植' '刘协' '王允' '袁尚' '高顺' '张绣' '刘启' '卞氏' '黄祖' '蔡氏'\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "def assign_group(people_group, group, group_name=''):\n",
    "    group = group.replace(\"'\", '')\n",
    "    group = group.split()\n",
    "    for people in group:\n",
    "        people_group[people] = group_name\n",
    "    return people_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "people_group = {}\n",
    "people_group = assign_group(people_group, wei, group_name='魏国')\n",
    "people_group = assign_group(people_group, shu, group_name='蜀国')\n",
    "people_group = assign_group(people_group, wu, group_name='吴国')\n",
    "people_group = assign_group(people_group, others, group_name='群雄')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'丁原': '群雄',\n",
       " '丁奉': '吴国',\n",
       " '乐进': '魏国',\n",
       " '于禁': '魏国',\n",
       " '公孙瓒': '群雄',\n",
       " '公孙越': '群雄',\n",
       " '关兴': '蜀国',\n",
       " '关平': '蜀国',\n",
       " '关羽': '蜀国',\n",
       " '典韦': '魏国',\n",
       " '凌统': '吴国',\n",
       " '刘协': '群雄',\n",
       " '刘启': '群雄',\n",
       " '刘备': '蜀国',\n",
       " '刘氏': '魏国',\n",
       " '刘禅': '蜀国',\n",
       " '刘胜': '群雄',\n",
       " '刘表': '群雄',\n",
       " '刘辩': '群雄',\n",
       " '卞氏': '群雄',\n",
       " '卢植': '群雄',\n",
       " '司马师': '魏国',\n",
       " '司马懿': '魏国',\n",
       " '司马昭': '魏国',\n",
       " '司马炎': '魏国',\n",
       " '吕伯奢': '群雄',\n",
       " '吕布': '群雄',\n",
       " '吕蒙': '吴国',\n",
       " '吴国太': '吴国',\n",
       " '周泰': '吴国',\n",
       " '周瑜': '吴国',\n",
       " '夏侯惇': '魏国',\n",
       " '夏侯楙': '魏国',\n",
       " '夏侯渊': '魏国',\n",
       " '大乔': '吴国',\n",
       " '太史慈': '吴国',\n",
       " '姜维': '蜀国',\n",
       " '孙坚': '吴国',\n",
       " '孙尚香': '吴国',\n",
       " '孙权': '吴国',\n",
       " '孙氏': '吴国',\n",
       " '孙策': '吴国',\n",
       " '孙韶': '吴国',\n",
       " '孟获': '群雄',\n",
       " '小乔': '吴国',\n",
       " '庞德': '魏国',\n",
       " '庞德公': '群雄',\n",
       " '庞统': '蜀国',\n",
       " '张宝': '群雄',\n",
       " '张昭': '吴国',\n",
       " '张梁': '群雄',\n",
       " '张绣': '群雄',\n",
       " '张苞': '蜀国',\n",
       " '张角': '群雄',\n",
       " '张辽': '魏国',\n",
       " '张郃': '魏国',\n",
       " '张飞': '蜀国',\n",
       " '徐庶': '蜀国',\n",
       " '徐晃': '魏国',\n",
       " '徐盛': '吴国',\n",
       " '文聘': '魏国',\n",
       " '曹丕': '吴国',\n",
       " '曹仁': '魏国',\n",
       " '曹嵩': '魏国',\n",
       " '曹操': '魏国',\n",
       " '曹昂': '魏国',\n",
       " '曹植': '魏国',\n",
       " '曹爽': '魏国',\n",
       " '曹真': '魏国',\n",
       " '曹纯': '魏国',\n",
       " '沙摩柯': '蜀国',\n",
       " '法正': '蜀国',\n",
       " '清河公主': '魏国',\n",
       " '王允': '群雄',\n",
       " '甘宁': '吴国',\n",
       " '甘氏': '蜀国',\n",
       " '祝融': '蜀国',\n",
       " '程昱': '魏国',\n",
       " '程普': '吴国',\n",
       " '糜氏': '蜀国',\n",
       " '糜竺': '蜀国',\n",
       " '糜芳': '蜀国',\n",
       " '荀彧': '魏国',\n",
       " '荀攸': '魏国',\n",
       " '董卓': '群雄',\n",
       " '蒋琬': '蜀国',\n",
       " '蒋钦': '吴国',\n",
       " '蒯越': '魏国',\n",
       " '蔡氏': '群雄',\n",
       " '蔡瑁': '魏国',\n",
       " '袁尚': '群雄',\n",
       " '袁术': '群雄',\n",
       " '袁熙': '群雄',\n",
       " '袁绍': '群雄',\n",
       " '袁谭': '群雄',\n",
       " '许褚': '魏国',\n",
       " '诸葛亮': '蜀国',\n",
       " '诸葛瑾': '蜀国',\n",
       " '诸葛瞻': '蜀国',\n",
       " '貂蝉': '群雄',\n",
       " '贾诩': '魏国',\n",
       " '赵云': '蜀国',\n",
       " '邓艾': '魏国',\n",
       " '邹氏': '魏国',\n",
       " '郭嘉': '魏国',\n",
       " '钟会': '魏国',\n",
       " '陆逊': '吴国',\n",
       " '陈宫': '群雄',\n",
       " '韩当': '吴国',\n",
       " '韩遂': '群雄',\n",
       " '马腾': '群雄',\n",
       " '马良': '蜀国',\n",
       " '马谡': '蜀国',\n",
       " '马超': '蜀国',\n",
       " '高顺': '群雄',\n",
       " '魏延': '蜀国',\n",
       " '鲁肃': '吴国',\n",
       " '黄忠': '蜀国',\n",
       " '黄承彦': '蜀国',\n",
       " '黄月英': '蜀国',\n",
       " '黄盖': '吴国',\n",
       " '黄祖': '群雄'}"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "122"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(people_group)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "人物数目一致。接下来创建两列，给每行的两个任务添加所属的阵营。直接使用map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['head_group'] = df['head'].map(lambda x:people_group[x])\n",
    "df['tail_group'] = df['tail'].map(lambda x:people_group[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>tail</th>\n",
       "      <th>relation</th>\n",
       "      <th>label</th>\n",
       "      <th>head_group</th>\n",
       "      <th>tail_group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>关羽</td>\n",
       "      <td>刘备</td>\n",
       "      <td>younger_sworn_brother</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张飞</td>\n",
       "      <td>刘备</td>\n",
       "      <td>younger_sworn_brother</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>关羽</td>\n",
       "      <td>张飞</td>\n",
       "      <td>elder_sworn_brother</td>\n",
       "      <td>义兄</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>张苞</td>\n",
       "      <td>张飞</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>关兴</td>\n",
       "      <td>关羽</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>关平</td>\n",
       "      <td>张苞</td>\n",
       "      <td>sworn</td>\n",
       "      <td>结拜</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>关平</td>\n",
       "      <td>关羽</td>\n",
       "      <td>adopted_son</td>\n",
       "      <td>义子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>卢植</td>\n",
       "      <td>刘备</td>\n",
       "      <td>master</td>\n",
       "      <td>师傅</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>公孙瓒</td>\n",
       "      <td>刘备</td>\n",
       "      <td>friend</td>\n",
       "      <td>朋友</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>甘氏</td>\n",
       "      <td>刘备</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>刘禅</td>\n",
       "      <td>甘氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>刘禅</td>\n",
       "      <td>son_in_law</td>\n",
       "      <td>女婿</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>姜维</td>\n",
       "      <td>master</td>\n",
       "      <td>师傅</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>刘备</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>lord</td>\n",
       "      <td>主公</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马师</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马懿</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>司马炎</td>\n",
       "      <td>司马昭</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>曹仁</td>\n",
       "      <td>曹操</td>\n",
       "      <td>same_clan</td>\n",
       "      <td>同族</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>曹纯</td>\n",
       "      <td>曹仁</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>曹操</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>刘氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>刘氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>卞氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>wife</td>\n",
       "      <td>妻子</td>\n",
       "      <td>群雄</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>曹丕</td>\n",
       "      <td>卞氏</td>\n",
       "      <td>son</td>\n",
       "      <td>儿子</td>\n",
       "      <td>吴国</td>\n",
       "      <td>群雄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>曹植</td>\n",
       "      <td>曹丕</td>\n",
       "      <td>younger_brother</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>马谡</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>colleague</td>\n",
       "      <td>同事</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150</th>\n",
       "      <td>马谡</td>\n",
       "      <td>刘备</td>\n",
       "      <td>minister</td>\n",
       "      <td>臣</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>孙坚</td>\n",
       "      <td>孙权</td>\n",
       "      <td>father</td>\n",
       "      <td>父亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152</th>\n",
       "      <td>吴国太</td>\n",
       "      <td>孙权</td>\n",
       "      <td>mother</td>\n",
       "      <td>母亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>153 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    head tail               relation label head_group tail_group\n",
       "0     关羽   刘备  younger_sworn_brother    义弟         蜀国         蜀国\n",
       "1     张飞   刘备  younger_sworn_brother    义弟         蜀国         蜀国\n",
       "2     关羽   张飞    elder_sworn_brother    义兄         蜀国         蜀国\n",
       "3     张苞   张飞                    son    儿子         蜀国         蜀国\n",
       "4     关兴   关羽                    son    儿子         蜀国         蜀国\n",
       "5     关平   张苞                  sworn    结拜         蜀国         蜀国\n",
       "6     关平   关羽            adopted_son    义子         蜀国         蜀国\n",
       "7     卢植   刘备                 master    师傅         群雄         蜀国\n",
       "8    公孙瓒   刘备                 friend    朋友         群雄         蜀国\n",
       "9     甘氏   刘备                   wife     妻         蜀国         蜀国\n",
       "10    刘禅   甘氏                    son    儿子         蜀国         蜀国\n",
       "11   诸葛瞻   刘禅             son_in_law    女婿         蜀国         蜀国\n",
       "12   诸葛瞻  诸葛亮                    son    儿子         蜀国         蜀国\n",
       "13   诸葛亮   姜维                 master    师傅         蜀国         蜀国\n",
       "14    刘备  诸葛亮                   lord    主公         蜀国         蜀国\n",
       "..   ...  ...                    ...   ...        ...        ...\n",
       "138  司马昭  司马师        younger_brother    弟弟         魏国         魏国\n",
       "139  司马昭  司马懿                    son    儿子         魏国         魏国\n",
       "140  司马炎  司马昭                    son    儿子         魏国         魏国\n",
       "141   曹仁   曹操              same_clan    同族         魏国         魏国\n",
       "142   曹纯   曹仁        younger_brother    弟弟         魏国         魏国\n",
       "143   曹昂   曹操                    son    儿子         魏国         魏国\n",
       "144   刘氏   曹操                   wife    妻子         魏国         魏国\n",
       "145   曹昂   刘氏                    son    儿子         魏国         魏国\n",
       "146   卞氏   曹操                   wife    妻子         群雄         魏国\n",
       "147   曹丕   卞氏                    son    儿子         吴国         群雄\n",
       "148   曹植   曹丕        younger_brother    弟弟         魏国         吴国\n",
       "149   马谡  诸葛亮              colleague    同事         蜀国         蜀国\n",
       "150   马谡   刘备               minister     臣         蜀国         蜀国\n",
       "151   孙坚   孙权                 father    父亲         吴国         吴国\n",
       "152  吴国太   孙权                 mother    母亲         吴国         吴国\n",
       "\n",
       "[153 rows x 6 columns]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后把relation这一列去掉就行了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.drop(['relation'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>tail</th>\n",
       "      <th>label</th>\n",
       "      <th>head_group</th>\n",
       "      <th>tail_group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>关羽</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张飞</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>关羽</td>\n",
       "      <td>张飞</td>\n",
       "      <td>义兄</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>张苞</td>\n",
       "      <td>张飞</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>关兴</td>\n",
       "      <td>关羽</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>关平</td>\n",
       "      <td>张苞</td>\n",
       "      <td>结拜</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>关平</td>\n",
       "      <td>关羽</td>\n",
       "      <td>义子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>卢植</td>\n",
       "      <td>刘备</td>\n",
       "      <td>师傅</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>公孙瓒</td>\n",
       "      <td>刘备</td>\n",
       "      <td>朋友</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>甘氏</td>\n",
       "      <td>刘备</td>\n",
       "      <td>妻</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>刘禅</td>\n",
       "      <td>甘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>刘禅</td>\n",
       "      <td>女婿</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>姜维</td>\n",
       "      <td>师傅</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>刘备</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>主公</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马师</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马懿</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>司马炎</td>\n",
       "      <td>司马昭</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>曹仁</td>\n",
       "      <td>曹操</td>\n",
       "      <td>同族</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>曹纯</td>\n",
       "      <td>曹仁</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>曹操</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>刘氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>刘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>卞氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>群雄</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>曹丕</td>\n",
       "      <td>卞氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>吴国</td>\n",
       "      <td>群雄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>曹植</td>\n",
       "      <td>曹丕</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>马谡</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>同事</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150</th>\n",
       "      <td>马谡</td>\n",
       "      <td>刘备</td>\n",
       "      <td>臣</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>孙坚</td>\n",
       "      <td>孙权</td>\n",
       "      <td>父亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152</th>\n",
       "      <td>吴国太</td>\n",
       "      <td>孙权</td>\n",
       "      <td>母亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>153 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    head tail label head_group tail_group\n",
       "0     关羽   刘备    义弟         蜀国         蜀国\n",
       "1     张飞   刘备    义弟         蜀国         蜀国\n",
       "2     关羽   张飞    义兄         蜀国         蜀国\n",
       "3     张苞   张飞    儿子         蜀国         蜀国\n",
       "4     关兴   关羽    儿子         蜀国         蜀国\n",
       "5     关平   张苞    结拜         蜀国         蜀国\n",
       "6     关平   关羽    义子         蜀国         蜀国\n",
       "7     卢植   刘备    师傅         群雄         蜀国\n",
       "8    公孙瓒   刘备    朋友         群雄         蜀国\n",
       "9     甘氏   刘备     妻         蜀国         蜀国\n",
       "10    刘禅   甘氏    儿子         蜀国         蜀国\n",
       "11   诸葛瞻   刘禅    女婿         蜀国         蜀国\n",
       "12   诸葛瞻  诸葛亮    儿子         蜀国         蜀国\n",
       "13   诸葛亮   姜维    师傅         蜀国         蜀国\n",
       "14    刘备  诸葛亮    主公         蜀国         蜀国\n",
       "..   ...  ...   ...        ...        ...\n",
       "138  司马昭  司马师    弟弟         魏国         魏国\n",
       "139  司马昭  司马懿    儿子         魏国         魏国\n",
       "140  司马炎  司马昭    儿子         魏国         魏国\n",
       "141   曹仁   曹操    同族         魏国         魏国\n",
       "142   曹纯   曹仁    弟弟         魏国         魏国\n",
       "143   曹昂   曹操    儿子         魏国         魏国\n",
       "144   刘氏   曹操    妻子         魏国         魏国\n",
       "145   曹昂   刘氏    儿子         魏国         魏国\n",
       "146   卞氏   曹操    妻子         群雄         魏国\n",
       "147   曹丕   卞氏    儿子         吴国         群雄\n",
       "148   曹植   曹丕    弟弟         魏国         吴国\n",
       "149   马谡  诸葛亮    同事         蜀国         蜀国\n",
       "150   马谡   刘备     臣         蜀国         蜀国\n",
       "151   孙坚   孙权    父亲         吴国         吴国\n",
       "152  吴国太   孙权    母亲         吴国         吴国\n",
       "\n",
       "[153 rows x 5 columns]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出所有的关系，之后针对这些关系设置一些近义词方便检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'主公',\n",
       " '义兄',\n",
       " '义女',\n",
       " '义子',\n",
       " '义弟',\n",
       " '义父',\n",
       " '儿子',\n",
       " '叔叔',\n",
       " '同事',\n",
       " '同族',\n",
       " '哥哥',\n",
       " '堂弟',\n",
       " '夫',\n",
       " '女儿',\n",
       " '女婿',\n",
       " '妹妹',\n",
       " '妻',\n",
       " '妻子',\n",
       " '妾',\n",
       " '嫂子',\n",
       " '岳母',\n",
       " '师傅',\n",
       " '弟弟',\n",
       " '徒弟',\n",
       " '朋友',\n",
       " '母亲',\n",
       " '父亲',\n",
       " '结拜',\n",
       " '臣',\n",
       " '让位',\n",
       " '诸侯'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(df.label.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 很不幸发现有几行是重复的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_relation(name):\n",
    "    p1 = df.loc[df['head'] == name]\n",
    "    p2 = df.loc[df['tail'] == name]\n",
    "    print(p1)\n",
    "    print(p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   head tail label head_group tail_group\n",
      "10   刘禅   甘氏    儿子         蜀国         蜀国\n",
      "29   刘禅   甘氏    儿子         蜀国         蜀国\n",
      "   head tail label head_group tail_group\n",
      "11  诸葛瞻   刘禅    女婿         蜀国         蜀国\n",
      "30  诸葛瞻   刘禅    女婿         蜀国         蜀国\n"
     ]
    }
   ],
   "source": [
    "find_relation( '刘禅' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>tail</th>\n",
       "      <th>label</th>\n",
       "      <th>head_group</th>\n",
       "      <th>tail_group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>关羽</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张飞</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>关羽</td>\n",
       "      <td>张飞</td>\n",
       "      <td>义兄</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>张苞</td>\n",
       "      <td>张飞</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>关兴</td>\n",
       "      <td>关羽</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>关平</td>\n",
       "      <td>张苞</td>\n",
       "      <td>结拜</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>关平</td>\n",
       "      <td>关羽</td>\n",
       "      <td>义子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>卢植</td>\n",
       "      <td>刘备</td>\n",
       "      <td>师傅</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>公孙瓒</td>\n",
       "      <td>刘备</td>\n",
       "      <td>朋友</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>甘氏</td>\n",
       "      <td>刘备</td>\n",
       "      <td>妻</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>刘禅</td>\n",
       "      <td>甘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>刘禅</td>\n",
       "      <td>女婿</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>姜维</td>\n",
       "      <td>师傅</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>刘备</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>主公</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马师</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马懿</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>司马炎</td>\n",
       "      <td>司马昭</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>曹仁</td>\n",
       "      <td>曹操</td>\n",
       "      <td>同族</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>曹纯</td>\n",
       "      <td>曹仁</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>曹操</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>刘氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>刘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>卞氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>群雄</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>曹丕</td>\n",
       "      <td>卞氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>吴国</td>\n",
       "      <td>群雄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>曹植</td>\n",
       "      <td>曹丕</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>马谡</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>同事</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150</th>\n",
       "      <td>马谡</td>\n",
       "      <td>刘备</td>\n",
       "      <td>臣</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>孙坚</td>\n",
       "      <td>孙权</td>\n",
       "      <td>父亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152</th>\n",
       "      <td>吴国太</td>\n",
       "      <td>孙权</td>\n",
       "      <td>母亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>153 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    head tail label head_group tail_group\n",
       "0     关羽   刘备    义弟         蜀国         蜀国\n",
       "1     张飞   刘备    义弟         蜀国         蜀国\n",
       "2     关羽   张飞    义兄         蜀国         蜀国\n",
       "3     张苞   张飞    儿子         蜀国         蜀国\n",
       "4     关兴   关羽    儿子         蜀国         蜀国\n",
       "5     关平   张苞    结拜         蜀国         蜀国\n",
       "6     关平   关羽    义子         蜀国         蜀国\n",
       "7     卢植   刘备    师傅         群雄         蜀国\n",
       "8    公孙瓒   刘备    朋友         群雄         蜀国\n",
       "9     甘氏   刘备     妻         蜀国         蜀国\n",
       "10    刘禅   甘氏    儿子         蜀国         蜀国\n",
       "11   诸葛瞻   刘禅    女婿         蜀国         蜀国\n",
       "12   诸葛瞻  诸葛亮    儿子         蜀国         蜀国\n",
       "13   诸葛亮   姜维    师傅         蜀国         蜀国\n",
       "14    刘备  诸葛亮    主公         蜀国         蜀国\n",
       "..   ...  ...   ...        ...        ...\n",
       "138  司马昭  司马师    弟弟         魏国         魏国\n",
       "139  司马昭  司马懿    儿子         魏国         魏国\n",
       "140  司马炎  司马昭    儿子         魏国         魏国\n",
       "141   曹仁   曹操    同族         魏国         魏国\n",
       "142   曹纯   曹仁    弟弟         魏国         魏国\n",
       "143   曹昂   曹操    儿子         魏国         魏国\n",
       "144   刘氏   曹操    妻子         魏国         魏国\n",
       "145   曹昂   刘氏    儿子         魏国         魏国\n",
       "146   卞氏   曹操    妻子         群雄         魏国\n",
       "147   曹丕   卞氏    儿子         吴国         群雄\n",
       "148   曹植   曹丕    弟弟         魏国         吴国\n",
       "149   马谡  诸葛亮    同事         蜀国         蜀国\n",
       "150   马谡   刘备     臣         蜀国         蜀国\n",
       "151   孙坚   孙权    父亲         吴国         吴国\n",
       "152  吴国太   孙权    母亲         吴国         吴国\n",
       "\n",
       "[153 rows x 5 columns]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除重复行\n",
    "df = df.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>tail</th>\n",
       "      <th>label</th>\n",
       "      <th>head_group</th>\n",
       "      <th>tail_group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>关羽</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>张飞</td>\n",
       "      <td>刘备</td>\n",
       "      <td>义弟</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>关羽</td>\n",
       "      <td>张飞</td>\n",
       "      <td>义兄</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>张苞</td>\n",
       "      <td>张飞</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>关兴</td>\n",
       "      <td>关羽</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>关平</td>\n",
       "      <td>张苞</td>\n",
       "      <td>结拜</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>关平</td>\n",
       "      <td>关羽</td>\n",
       "      <td>义子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>卢植</td>\n",
       "      <td>刘备</td>\n",
       "      <td>师傅</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>公孙瓒</td>\n",
       "      <td>刘备</td>\n",
       "      <td>朋友</td>\n",
       "      <td>群雄</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>甘氏</td>\n",
       "      <td>刘备</td>\n",
       "      <td>妻</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>刘禅</td>\n",
       "      <td>甘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>刘禅</td>\n",
       "      <td>女婿</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>诸葛瞻</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>儿子</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>姜维</td>\n",
       "      <td>师傅</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>刘备</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>主公</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马师</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>司马昭</td>\n",
       "      <td>司马懿</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>司马炎</td>\n",
       "      <td>司马昭</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>曹仁</td>\n",
       "      <td>曹操</td>\n",
       "      <td>同族</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>曹纯</td>\n",
       "      <td>曹仁</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>曹操</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>刘氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>曹昂</td>\n",
       "      <td>刘氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>魏国</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>卞氏</td>\n",
       "      <td>曹操</td>\n",
       "      <td>妻子</td>\n",
       "      <td>群雄</td>\n",
       "      <td>魏国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>曹丕</td>\n",
       "      <td>卞氏</td>\n",
       "      <td>儿子</td>\n",
       "      <td>吴国</td>\n",
       "      <td>群雄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>曹植</td>\n",
       "      <td>曹丕</td>\n",
       "      <td>弟弟</td>\n",
       "      <td>魏国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>马谡</td>\n",
       "      <td>诸葛亮</td>\n",
       "      <td>同事</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150</th>\n",
       "      <td>马谡</td>\n",
       "      <td>刘备</td>\n",
       "      <td>臣</td>\n",
       "      <td>蜀国</td>\n",
       "      <td>蜀国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>孙坚</td>\n",
       "      <td>孙权</td>\n",
       "      <td>父亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152</th>\n",
       "      <td>吴国太</td>\n",
       "      <td>孙权</td>\n",
       "      <td>母亲</td>\n",
       "      <td>吴国</td>\n",
       "      <td>吴国</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>146 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    head tail label head_group tail_group\n",
       "0     关羽   刘备    义弟         蜀国         蜀国\n",
       "1     张飞   刘备    义弟         蜀国         蜀国\n",
       "2     关羽   张飞    义兄         蜀国         蜀国\n",
       "3     张苞   张飞    儿子         蜀国         蜀国\n",
       "4     关兴   关羽    儿子         蜀国         蜀国\n",
       "5     关平   张苞    结拜         蜀国         蜀国\n",
       "6     关平   关羽    义子         蜀国         蜀国\n",
       "7     卢植   刘备    师傅         群雄         蜀国\n",
       "8    公孙瓒   刘备    朋友         群雄         蜀国\n",
       "9     甘氏   刘备     妻         蜀国         蜀国\n",
       "10    刘禅   甘氏    儿子         蜀国         蜀国\n",
       "11   诸葛瞻   刘禅    女婿         蜀国         蜀国\n",
       "12   诸葛瞻  诸葛亮    儿子         蜀国         蜀国\n",
       "13   诸葛亮   姜维    师傅         蜀国         蜀国\n",
       "14    刘备  诸葛亮    主公         蜀国         蜀国\n",
       "..   ...  ...   ...        ...        ...\n",
       "138  司马昭  司马师    弟弟         魏国         魏国\n",
       "139  司马昭  司马懿    儿子         魏国         魏国\n",
       "140  司马炎  司马昭    儿子         魏国         魏国\n",
       "141   曹仁   曹操    同族         魏国         魏国\n",
       "142   曹纯   曹仁    弟弟         魏国         魏国\n",
       "143   曹昂   曹操    儿子         魏国         魏国\n",
       "144   刘氏   曹操    妻子         魏国         魏国\n",
       "145   曹昂   刘氏    儿子         魏国         魏国\n",
       "146   卞氏   曹操    妻子         群雄         魏国\n",
       "147   曹丕   卞氏    儿子         吴国         群雄\n",
       "148   曹植   曹丕    弟弟         魏国         吴国\n",
       "149   马谡  诸葛亮    同事         蜀国         蜀国\n",
       "150   马谡   刘备     臣         蜀国         蜀国\n",
       "151   孙坚   孙权    父亲         吴国         吴国\n",
       "152  吴国太   孙权    母亲         吴国         吴国\n",
       "\n",
       "[146 rows x 5 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../raw_data/triples_processed.txt'\n",
    "df.to_csv(path, header=None, index=None, sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:py36]",
   "language": "python",
   "name": "conda-env-py36-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
